package com.sixth.controller;

import com.github.pagehelper.util.StringUtil;
import com.sixth.bean.MarketCoupon;
import com.sixth.bean.MarketUser;
import com.sixth.bean.bo.CouponCodeBO;
import com.sixth.bean.bo.CouponPageInfo;
import com.sixth.bean.bo.ReceiveCouponBO;
import com.sixth.bean.common.BasePageInfo;
import com.sixth.bean.common.BasePageInfoVo;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.vo.BaseListVO;
import com.sixth.bean.vo.UserCouponVO;
import com.sixth.constant.Constant;
import com.sixth.service.CouponService;
import com.sixth.util.MarketInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @Author Chen
 * @Date 2022-09-10  13-49-50
 * @description  小程序优惠券管理
 */
@RestController
@RequestMapping("wx/coupon")
public class WXCouponController {


    @Autowired
    CouponService couponService;

    //用户领取优惠券
    @PostMapping("receive")
    public BaseRespVo receiveCoupon(@RequestBody ReceiveCouponBO receiveCouponBO){

        MarketUser marketUser = MarketInfoUtil.getMarketUser();
        if (marketUser==null){

            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(Constant.NOTLOGIN);
            baseRespVo.setErrmsg("请先登录");
            return baseRespVo;
        }


        int receiveCode = couponService.receiveCoupon(receiveCouponBO.getCouponId(),marketUser.getId());

        if (receiveCode == Constant.OK){
            return BaseRespVo.ok("领取成功");
        }

        if (receiveCode == Constant.RECEIVED) {

            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(Constant.RECEIVED);
            baseRespVo.setErrmsg("优惠券已经领取过");
            return baseRespVo;
        }

        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrno(Constant.RECEIVED);
        baseRespVo.setErrmsg("该活动已结束");
        return baseRespVo;
    }


    // 客户端应用查询优惠券
    @GetMapping("list")
    public BaseRespVo getCouponList(Integer page,Integer limit){


        CouponPageInfo couponPageInfo = new CouponPageInfo();
        couponPageInfo.setPage(page);
        couponPageInfo.setLimit(limit);

        BaseListVO<MarketCoupon> couponList = couponService.getWXCouponList(couponPageInfo);

        if (couponList.getList().size()>=Constant.ONE){
            return BaseRespVo.ok(couponList);
        }


        return BaseRespVo.invalidData("暂时没有优惠券，等待下次活动吧");
    }


    // 客户端用户查询优惠券
    @GetMapping("mylist")
    public BaseRespVo getMyList(Integer page,Integer limit,Integer status){
        MarketUser marketUser = MarketInfoUtil.getMarketUser();
        if (marketUser==null){
            return BaseRespVo.invalidData("请先登录");
        }

        CouponPageInfo couponPageInfo = new CouponPageInfo();
        couponPageInfo.setPage(page);
        couponPageInfo.setLimit(limit);
        couponPageInfo.setStatus(status);

        BasePageInfoVo<UserCouponVO> userCouponList = (BasePageInfoVo<UserCouponVO>)couponService.getMyCoupons(couponPageInfo,marketUser.getId());

        if (userCouponList!=null&&userCouponList.getList().size()>=Constant.ONE){

            return BaseRespVo.ok(userCouponList);
        }

        return BaseRespVo.invalidData("暂无优惠券");
    }


    /**
     * @Author Chen
     * @Date 2022-09-12  11-03-39
     * @param
     * @return {@link null }
     * @description  用户通过兑换码兑换优惠券
     */

    @PostMapping("exchange")
    public BaseRespVo exchangeCoupon(@RequestBody CouponCodeBO couponCodeBO){

        int exchangeCode = couponService.exchangeCoupon(couponCodeBO);

        if (exchangeCode == Constant.OK){
            return BaseRespVo.ok("兑换成功",Constant.ZERO);
        }

        return BaseRespVo.invalidParameter("兑换码有误");
    }


    /**
     * @Author Chen
     * @Date 2022-09-12  15-58-58
     * @param
     * @return {@link null }
     * @description 用户下单时，选择使用那一个优惠券,只显示当前商品可用的优惠券
     */
    @GetMapping("selectlist")
    public BaseRespVo selectList(Integer cartId,Integer grouponRulesId){

        BaseListVO<UserCouponVO> enableCoupons = couponService.selectList(cartId);
        if (enableCoupons!=null&&enableCoupons.getList().size()>=Constant.ONE){
            return BaseRespVo.ok(enableCoupons,0);
        }

        return BaseRespVo.invalidData("暂无优惠券");
    }
}
